﻿<%@ codepage=65001%><% 'UTF-8%>
<% '会员卡管理%>
<!--#include file="../Include/Common.All.asp"-->
<!--#include file="../Include/Conn.asp"-->
<% call OpenConn%>

<!--#include file="../Include/Common.Security.asp"-->
<!--#include file="../Include/MD5.asp"-->
<!--#include file="../Include/JSON_2.0.2.asp"-->
<!--#include file="../Include/JSON_UTIL_0.1.1.asp"-->

<%
	'定义json对象
	Dim jsonObj,jsonObj_user
	Set jsonObj = jsObject()
	Set jsonObj_user = jsObject()
	UserID=ReplaceBadChar(Trim(Session("UserID")))
	
	dim act 
  act=request("act")
 	Dim rsObj,strSql
	Dim page ''当前查询页
	Dim RowCount''影响的记录条数
	''查询信息
	''[卡号,商品条形码,商品名称,生产厂商,规格,商标,生产批次,生产日期,到期时间,数量,单价,商店代码,购买日期,经办人,操作]}
	dim cid,Cardcode,Spcode,spname,sccs,gg			
	dim shangbiao,scpc,scrq,dqsj,sl			
	dim dj,qycode,gmrq,jbr
 
	''查询信息列表
	if "search"=act then
		page=ReplaceBadChar(Trim(request.Form("page")))&""
		
		cid=ReplaceBadChar(Trim(request.Form("cid")))''id 序列号
		cardcode=ReplaceBadChar(Trim(request.Form("cardcode")))
		Spcode=ReplaceBadChar(Trim(request.Form("Spcode")))
		spname=ReplaceBadChar(Trim(request.Form("spname")))
				
		jsonObj("rslist") = ""
		''添加用户信息
		jsonObj_user("UserID") = UserID 
		jsonObj_user("UserName") =ReplaceBadChar(Trim(Session("UserName")))
		Set jsonObj("customer") = jsonObj_user
	
	 
		''处理key值和value的对应关系 eg:"cardtype":[{"key":"0","value":"\u5931\u6548"},{"key":"1","value":"\u751F\u6548"}]
		Dim jsonObj_map 
		Set jsonObj_map = jsArray()
		
		set jsonObj_map(0) = jsObject()
		jsonObj_map(0)("key") = "0"
		jsonObj_map(0)("value") = "未开卡"
		set jsonObj_map(1) = jsObject()
		jsonObj_map(1) ("key") = "1"
		jsonObj_map(1) ("value") = "生效"
		set jsonObj_map(2) = jsObject()
		jsonObj_map(2) ("key") = "2"
		jsonObj_map(2) ("value") = "锁定"
		set jsonObj_map(3) = jsObject()
		jsonObj_map(3) ("key") = "3"
		jsonObj_map(3) ("value") = "作废"	
		Set jsonObj("cardtype") =  jsonObj_map''用户卡状态
		
		Set jsonObj_map = jsArray()
		set jsonObj_map(1) = jsObject()
		jsonObj_map(1)("key") = "1"
		jsonObj_map(1)("value") = "POS"
		set jsonObj_map(2) = jsObject()
		jsonObj_map(2) ("key") = "2"
		jsonObj_map(2) ("value") = "个人"
		Set jsonObj("map_flag") =  jsonObj_map''数据来源

		Set jsonObj_map = jsArray()
		set jsonObj_map(1) = jsObject()
		jsonObj_map(1)("key") = "1"
		jsonObj_map(1)("value") = "正常"
		set jsonObj_map(2) = jsObject()
		jsonObj_map(2) ("key") = "2"
		jsonObj_map(2) ("value") = "申请退货"
		set jsonObj_map(3) = jsObject()
		jsonObj_map(3) ("key") = "3"
		jsonObj_map(3) ("value") = "退货成功"
		Set jsonObj("map_state") =  jsonObj_map''数据来源


		''处理key值和value的对应关系--end
	
		If ("" = UserID ) Then UserID = "-999hasd9huoad9e993jwoi982niuh9wncdisj99"
	 
		''添加查询结果信息
		''Dim rsObj,strSql
		strSql = " SELECT top 100 PERCENT HYKXH.cardcode as orderid1,HYKXH.*  "  
		strSql =  strSql & " ,HYKTH.hykthid AS hykthid, HYKTH.thyy AS thyy, HYKTH.shcl AS shcl  "		
		strSql =  strSql & " ,HYKTH.remarks AS thremarks, HYKTH.sqthrq AS sqthrq "		
		strSql =  strSql & " FROM HYKXH INNER JOIN  HY_KH ON HYKXH.Cardcode = HY_KH.cardcode "		
		strSql =  strSql & " LEFT OUTER JOIN   HYKTH ON HYKXH.id = HYKTH.hykxhid "
		strSql =  strSql & " WHERE (HYKXH.state = '2' Or HYKXH.state = '3') "
		strSql =  strSql & " And HY_KH.Hycode = '"&UserID&"' "
		if (""<>cid) then strSql =  strSql & " and HYKXH.id = '" & cid & "'  " end if
		if (""<>Spcode) then strSql =  strSql & " and HYKXH.Spcode like '%" & Spcode & "%'  " end if
		if (""<>spname) then strSql =  strSql & " and HYKXH.spname like '%" & spname & "%'  " end if
		if (""<>cardcode) then strSql =  strSql & " and HYKXH.cardcode like '%" & cardcode & "%'  " end if
		strSql =  strSql & " ORDER BY HYKXH.orderid1 "

 		dim TotalRsNum ,PageSizeCur''TotalRsNum总记录数''PageSizeCur当前页记录数
		strSqlTemp = "select count(*)  from ( " & strSql & " ) kksds "
		TotalRsNum = PE_CLng(Conn.Execute(strSqlTemp)(0))
		''PageSize 页的记录数量
		PageSizeCur = PageSize ''当前页记录数
		if (PageSize*PE_CLng(page)>TotalRsNum or 0=TotalRsNum) then ''判断最后一页记录数是否和PageSize相等
			PageSizeCur= TotalRsNum Mod PageSize ''该页的时间记录数
		end if

		strSqlTemp = " SELECT ttkkk.* FROM (SELECT TOP " & CStr(PageSizeCur)
		strSqlTemp = strSqlTemp & "  mmsss.* FROM (SELECT TOP " & CStr(PageSize*PE_CLng(page)) & " nnfff.* FROM ( " 
		strSqlTemp = strSqlTemp &	strSql  
		strSqlTemp = strSqlTemp &  " ) nnfff) mmsss  ORDER BY mmsss.orderid1 DESC) ttkkk ORDER BY ttkkk.orderid1"

		Set jsonObj("rslist") = QueryToJSON(Conn, strSqlTemp)
		
		''添加分页信息
		'定义json对象
		Dim jsonObj_page
		Set jsonObj_page = jsObject()
			jsonObj_page("PageCur") = Page ''当前页	
			jsonObj_page("PageSizeCur") = PageSizeCur ''当前页记录数
			if (0=TotalRsNum  mod PageSize  ) then
				jsonObj_page("PageTotalNum")= TotalRsNum\PageSize''总页数
			else
				jsonObj_page("PageTotalNum")= TotalRsNum\PageSize+1''总页数
			end if
		Set jsonObj("page") = jsonObj_page 
		jsonObj("success") = 1 'false
	  jsonObj("msg") = "记录查询成功！"
	 
	 
 elseif "return"=act then
''申请退货   取消退货
	Dim thyy ,state
		cid=ReplaceBadChar(Trim(request.Form("cid"))) ''订单id
		thyy=ReplaceBadChar(Trim(request.Form("thyy")))''退货原因 
		state=ReplaceBadChar(Trim(request.Form("state")))''订单状态 

	  RowCount = 0
	  if ("1"=state) then''申请退货
	    If (cid <> ""   ) Then 
	    	strSql = " UPDATE HYKXH SET state = '2' WHERE (id = "&cid&") "	    	
	    	Conn.Execute (strSql), RowCount
	    	if (1= RowCount) then 
	    			dim max_num_th

						''Dim rsObj,strSql
						strSql = " select ISNULL(MAX(hykthid), 0) as 'max_hykthid' from HYKTH "
						max_num_th = Conn.Execute(strSql)(0)+1

						set rsObj=Server.CreateObject("ADODB.Recordset")
						strSql = "Select  * from HYKTH where 1=2 "
						rsObj.Open strSql, Conn, 1, 3
						If rsObj.BOF And rsObj.EOF Then
							rsObj.AddNew '如果没记录新增，有记录update	
						end If
						rsObj("hykthid") = max_num_th ''退货表id号
						rsObj("hykxhid") = PE_CLng(cid) ''消费表id号
				  	rsObj("remarks") = "申请退货"
				  	rsObj("thyy") = thyy ''申请退货原因
						rsObj.Update
						rsObj.close
						Set rsObj = Nothing
						RowCount=2
	    	 End If''增加退货记录
	    End If'''修改状态为 申请退货
	    If RowCount = 2 Then
	        jsonObj("msg") = "申请退货成功，正在跳转..."
					jsonObj("success") = 1'"success"
					jsonObj("url") = "consumption_my_return.asp"
	    Else
	        jsonObj("success") = 0 'false
	        jsonObj("msg") = "申请退货失败。"
	    End If
    Elseif ("2"= state) then ''取消退货
    		dim RowCountfff
    		''修改订单状态
	    	strSql = " UPDATE HYKXH SET state = '1' WHERE (id = "&cid&") "	    	
	    	Conn.Execute (strSql), RowCount
	    	RowCountfff = RowCount
				''删除退货记录
	    	strSql = " Delete From HYKTH WHERE (hykxhid = "&cid&") "	    	
	    	Conn.Execute (strSql), RowCount
	    	RowCountfff = RowCountfff + RowCount
		    If RowCountfff = 2 Then
		        jsonObj("msg") = "取消退货成功，正在跳转..."
						jsonObj("success") = 1'"success"
						jsonObj("url") = "consumption_my_return.asp"
		    Else
		        jsonObj("success") = 0 'false
		        jsonObj("msg") = "取消退货失败。"
		    End If 
    End If
	    
 
		 
  	End If

 
 

	'输出json对象
	jsonObj.Flush
	''Response.Write strSql&"-----"
 
	call CloseConn

''{
''    rslist : [ { name: "mac", desc: "computer",     
''                   price: 1000, quantity: 100, alert:null },
''                 { name: "ipod", desc: "music player", 
''                   price:  200, quantity: 200, alert:"on sale now!" },
''                 { name: "cinema display", desc: "screen",       
''                   price:  800, quantity: 300, alert:"best deal!" } ],
''    customer : { first: "John", last: "Public", level: "gold" }
''}

%>